package format

import (
	"strconv"
	"strings"
	"regexp"
	"encoding/json"
	"math"
)

func leagueData(text string) (bool,[]float64) {

	s:=strings.Split(text,"$$$$")

	ll:=strings.Split(s[0],"$$")

	if len(ll)<2||!strings.Contains(ll[0],"^")||!strings.Contains(ll[1],"^"){
		return false,nil
	}

	home:=strings.Split(ll[0],"!")
	away:=strings.Split(ll[1],"!")


	//主队数据
	homeAllSlice:=strings.Split(home[0],"^")

	homeAllWin,_:=strconv.ParseFloat(homeAllSlice[2],64)
	homeAllDraw,_:=strconv.ParseFloat(homeAllSlice[3],64)
	homeAllLoss,_:=strconv.ParseFloat(homeAllSlice[4],64)
	if (homeAllWin+homeAllDraw+homeAllLoss)==0{
		return false,nil
	}
	//主队胜率
	homeAllWinPercent:=homeAllWin/(homeAllWin+homeAllDraw+homeAllLoss)
	//主队平率
	homeAllDrawPercent:=homeAllDraw/(homeAllWin+homeAllDraw+homeAllLoss)
	//主队负率
	homeAllLossPercent:=homeAllLoss/(homeAllWin+homeAllDraw+homeAllLoss)

	//主队主场数据
	homeHomeSlice:=strings.Split(home[1],"^")

	homeHomeWin,_:=strconv.ParseFloat(homeHomeSlice[2],64)
	homeHomeDraw,_:=strconv.ParseFloat(homeHomeSlice[3],64)
	homeHomeLoss,_:=strconv.ParseFloat(homeHomeSlice[4],64)

	if (homeHomeWin+homeHomeDraw+homeHomeLoss)==0{
		return false,nil
	}
	//主队主场胜率
	homeHomeWinPercent:=homeHomeWin/(homeHomeWin+homeHomeDraw+homeHomeLoss)
	//主队主场平率
	homeHomeDrawPercent:=homeHomeDraw/(homeHomeWin+homeHomeDraw+homeHomeLoss)
	//主队主场负率
	homeHomeLossPercent:=homeHomeLoss/(homeHomeWin+homeHomeDraw+homeHomeLoss)

	//客场数据
	awayAllSlice:=strings.Split(away[0],"^")

	awayAllWin,_:=strconv.ParseFloat(awayAllSlice[2],64)
	awayAllDraw,_:=strconv.ParseFloat(awayAllSlice[3],64)
	awayAllLoss,_:=strconv.ParseFloat(awayAllSlice[4],64)
	if (awayAllWin+awayAllDraw+awayAllLoss)==0{
		return false,nil
	}
	//客队胜率
	awayAllWinPercent:=awayAllWin/(awayAllWin+awayAllDraw+awayAllLoss)
	//客队平率
	awayAllDrawPercent:=awayAllDraw/(awayAllWin+awayAllDraw+awayAllLoss)
	//客队负率
	awayAllLossPercent:=awayAllLoss/(awayAllWin+awayAllDraw+awayAllLoss)

	//客队客场数据
	awayAwaySlice:=strings.Split(away[2],"^")

	awayAwayWin,_:=strconv.ParseFloat(awayAwaySlice[2],64)
	awayAwayDraw,_:=strconv.ParseFloat(awayAwaySlice[3],64)
	awayAwayLoss,_:=strconv.ParseFloat(awayAwaySlice[4],64)
	if (awayAwayWin+awayAwayDraw+awayAwayLoss)==0{
		return false,nil
	}
	//客队客场胜率
	awayAwayWinPercent:=awayAwayWin/(awayAwayWin+awayAwayDraw+awayAwayLoss)
	//客队客场平率
	awayAwayDrawPercent:=awayAwayDraw/(awayAwayWin+awayAwayDraw+awayAwayLoss)
	//客队客场负率
	awayAwayLossPercent:=awayAwayLoss/(awayAwayWin+awayAwayDraw+awayAwayLoss)

	//主队得分能力
	homeAllWinScore,_:=strconv.ParseFloat(homeAllSlice[5],64)
	homeAllLossScore,_:=strconv.ParseFloat(homeAllSlice[6],64)
	homeAllScore:=homeAllWinScore/(homeAllWinScore+homeAllLossScore)
	//主队主场得分能力
	homeHomeWinScore,_:=strconv.ParseFloat(homeHomeSlice[5],64)
	homeHomeLossScore,_:=strconv.ParseFloat(homeHomeSlice[6],64)
	homeHomeScore:=homeHomeWinScore/(homeHomeWinScore+homeHomeLossScore)

	//客队得分能力
	awayAllWinScore,_:=strconv.ParseFloat(awayAllSlice[5],64)
	awayAllLossScore,_:=strconv.ParseFloat(awayAllSlice[6],64)
	awayAllScore:=awayAllWinScore/(awayAllWinScore+awayAllLossScore)
	//客队客场得分能力
	awayAwayWinScore,_:=strconv.ParseFloat(awayAwaySlice[5],64)
	awayAwayLossScore,_:=strconv.ParseFloat(awayAwaySlice[6],64)
	awayAwayScore:=awayAwayWinScore/(awayAwayWinScore+awayAwayLossScore)

	//联赛排名
	homeRank,_:=strconv.ParseFloat(homeAllSlice[8],64)
	awayRank,_:=strconv.ParseFloat(awayAllSlice[8],64)

	return true,[]float64{
		homeAllWinPercent,awayAllWinPercent,
		homeAllDrawPercent,awayAllDrawPercent,
		homeAllLossPercent,awayAllLossPercent,
		homeHomeWinPercent,awayAwayWinPercent,
		homeHomeDrawPercent,awayAwayDrawPercent,
		homeHomeLossPercent,awayAwayLossPercent,
		homeAllScore,awayAllScore,
		homeHomeScore,awayAwayScore,
		homeRank-awayRank,
	}
}
/**
交战数据
[]f 总胜率，近5，近3，近1
 */
func betData(startAt string,t1 string,t2 string,text string) (bool,[]float64) {
	reg, _ := regexp.Compile("\\[[\\S\\s]+?]")
	//主队
	t1= reg.ReplaceAllString(t1, "")
	//客队
	t2= reg.ReplaceAllString(t2, "")
	s:=strings.Split(text,"$$$$")

	m:=strings.Split(s[1],"$$")
	//最近交锋数据处理
	bet:=strings.Split(m[0],"!")

	betWin:=float64(0)
	betFit:=float64(0)
	betLoss:=float64(0)
	scoreTotal:=float64(0)
	scoreHome:=float64(0)
	scoreGuest:=float64(0)
	betTotal:=float64(0)

	//赢盘
	wpk:=float64(0)
	dpk:=float64(0)
	lpk:=float64(0)
	//均盘口
	pkTotal:=float64(0)
	//最深盘
	pkMax:=float64(0)
	//最浅盘
	pkMin:=float64(9999)
	pkMaxCount:=float64(0)
	pkMinCount:=float64(0)
	for _,v:=range bet{

		slice:=strings.Split(v,"^")

		if v==""||len(slice)<11||slice[10]==""{
			continue
		}

		k:=slice[10]
		if k=="W"{
			wpk++
		}
		if k=="D"{
			dpk++
		}
		if k=="L"{
			lpk++
		}
		pk,_:=strconv.ParseFloat(slice[11],64)

		/*
		//比赛时间不超过一年
		time1,_:=time.Parse("20060102150405",slice[0])
		time2,_:=time.Parse("06-01-02 15:04",startAt)

		if time2.Sub(time1).Hours()>24*365{
			continue
		}
		if time2.Year()-time1.Year()>1{
			continue
		}*/


		ta:=slice[2]
		sa,_:=strconv.ParseFloat(slice[4],64)
		sb,_:=strconv.ParseFloat(slice[5],64)
		scoreTotal=scoreTotal+sa+sb
		if strings.Index(ta,t1)>=0{
			//同主客
			if math.Abs(pk)>math.Abs(pkMax){
				pkMax=pk
				pkMaxCount++
			}
			if math.Abs(pk)<math.Abs(pkMin){
				pkMin=pk
				pkMinCount++
			}
			pkTotal+=pk

			scoreHome=scoreHome+sa
			scoreGuest=scoreGuest+sb
			if sa>sb{
				//赢
				betWin++
			}
			if sa==sb{
				betFit++
			}
			if sa<sb{
				betLoss++
			}

		}else{
			//主客相反
			if pk!=0{
				pk=pk*(-1)
			}

			if math.Abs(pk)>math.Abs(pkMax){
				pkMax=pk
				pkMaxCount++
			}
			if math.Abs(pk)<math.Abs(pkMin){
				pkMin=pk
				pkMinCount++
			}
			pkTotal+=pk

			scoreHome=scoreHome+sb
			scoreGuest=scoreGuest+sa
			if sa<sb{
				betWin++
			}
			if sa==sb{
				betFit++
			}
			if sa>sb{
				betLoss++
			}

		}
		betTotal++
	}


	//交锋胜率
	f1:=float64(0)
	//交锋平率
	f2:=float64(0)
	//交锋负率
	f3:=float64(0)
	//交锋进球数占比差
	f4:=float64(0)
	//pkAvg:=pkTotal/betTotal
	//赢盘率
	f5:=wpk/(wpk+dpk+lpk)
	f6:=(wpk+dpk)/(wpk+dpk+lpk)
	if betTotal>2{
		f1=betWin/betTotal
		f2=betFit/betTotal
		f3=betLoss/betTotal
		if scoreTotal>0{
			f4=(scoreHome-scoreGuest)/scoreTotal
		}
		if pkMaxCount==0{
			pkMax=float64(-9999)
		}
		if pkMinCount==0{
			pkMin=float64(-9999)
		}
		return true,[]float64{f1,f2,f3,f4,pkMax,pkMin,f5,f6}

	}
	return false,nil
}


func lastMatchData(league string,t1 string,t2 string,text string) (bool,[][]float64) {

	reg, _ := regexp.Compile("\\[[\\S\\s]+?]")
	//主队
	t1= reg.ReplaceAllString(t1, "")
	//客队
	t2= reg.ReplaceAllString(t2, "")
	s := strings.Split(text, "BaseInfo")[1]
	jsonString := "{\"BaseInfo" + s
	data:=make(map[string]interface{})
	json.Unmarshal([]byte(jsonString),&data)
	nearInfo:=data["NearInfo"].(map[string]interface{})
	guestInfo:=nearInfo["GuestInfo"].([]interface{})
	homeInfo:=nearInfo["HomeInfo"].([]interface{})


	f1:=make([]float64,0)
	f2:=make([]float64,0)
	i1:=0
	i2:=0

	for _,v:=range homeInfo{
		if i1==6{
			break
		}
		d:=v.(map[string]interface{})
		if d["SclassName"]!=league{
			//continue
		}
		isHome:=false

		if strings.Index(d["HomeTeam"].(string),t1)>=0{
			isHome=true
		}
		ff:=d["HomeScore"].(float64)-d["GuestScore"].(float64)
		if (isHome&&ff<0)||(!isHome&&ff>0){
			//输
			f1=append(f1,float64(2))
		}else if ff==0{
			//平
			f1=append(f1,float64(1))
		}else{
			//赢
			f1=append(f1,float64(0))
		}
		i1++
	}


	for _,v:=range guestInfo{
		if i2==6{
			break
		}

		d:=v.(map[string]interface{})
		if d["SclassName"]!=league{
			//continue
		}
		isHome:=false

		if strings.Index(d["HomeTeam"].(string),t2)>=0{
			isHome=true
		}
		ff:=d["HomeScore"].(float64)-d["GuestScore"].(float64)
		if (isHome&&ff<0)||(!isHome&&ff>0){
			//输
			f2=append(f2,float64(2))
		}else if ff==0{
			//平
			f2=append(f2,float64(1))
		}else{
			//赢
			f2=append(f2,float64(0))
		}
		i2++
	}
	if i1<6||i2<6 {
		return false, nil
	}
	isHomeContWin:=float64(0)
	isAwayContWin:=float64(0)
	isHomeContLoss:=float64(0)
	isAwayContLoss:=float64(0)
	if f1[0]==0&&f1[1]==0&&f1[2]==0{
		isHomeContWin=float64(1)
	}
	if f2[0]==0&&f2[1]==0&&f2[2]==0{
		isAwayContWin=float64(1)
	}
	if f1[0]==2&&f1[1]==2&&f1[2]==2{
		isHomeContLoss=float64(1)
	}
	if f2[0]==2&&f2[1]==2&&f2[2]==2{
		isAwayContLoss=float64(1)
	}
	f3:=[]float64{
		isHomeContWin,
		isAwayContWin,
		isHomeContLoss,
		isAwayContLoss,
	}
	return true,[][]float64{f1,f2,f3}
}
func europeData(data string) (bool,[]float64) {
	slice := make([]string, 0)
	json.Unmarshal([]byte(data), &slice)
	f1:=float64(0)
	f2:=float64(0)
	f3:=float64(0)
	f4:=float64(0)
	f5:=float64(0)
	f6:=float64(0)

	for _, v := range slice {
		d := strings.Split(v, "|")

		size:=len(d)
		if d[size-3]=="威廉希尔(英国)"{
			f4, _ = strconv.ParseFloat(d[3], 64)
			f5, _ = strconv.ParseFloat(d[4], 64)
			f6, _ = strconv.ParseFloat(d[5], 64)
			f1, _ = strconv.ParseFloat(d[10], 64)
			f2, _ = strconv.ParseFloat(d[11], 64)
			f3, _ = strconv.ParseFloat(d[12], 64)

		}

	}
	if f1==0{
		return false,nil
	}
	return true,[]float64{f4,f5,f6,f1,f2,f3}
}
func bfData(bf string) (bool,[]float64) {
	if !strings.Contains(bf,"BFOdds"){
		return false,nil
	}
	bfJson := make(map[string]interface{})

	json.Unmarshal([]byte(bf), &bfJson)
	bfOdds:=bfJson["BFOdds"].(map[string]interface{})
	bl_home:=bfOdds["bl_home"].(float64)
	bl_draw:=bfOdds["bl_draw"].(float64)
	bl_guest:=bfOdds["bl_guest"].(float64)

	yk_home:=bfOdds["yk_home"].(float64)
	yk_draw:=bfOdds["yk_draw"].(float64)
	yk_guest:=bfOdds["yk_guest"].(float64)

	lr_home:=bfOdds["lr_home"].(float64)
	lr_draw:=bfOdds["lr_draw"].(float64)
	lr_guest:=bfOdds["lr_guest"].(float64)

	return true,[]float64{
		bl_home,bl_draw,bl_guest,
		yk_home,yk_draw,yk_guest,
		lr_home,lr_draw,lr_guest,
	}
}
func liveData(live string) (bool,[]float64) {
	if live==""{
		return false,nil
	}
	liveJson := make(map[string]interface{})

	json.Unmarshal([]byte(live), &liveJson)
	if liveJson["listItems"]==nil{
		return false,nil
	}
	listItems:=liveJson["listItems"].([]interface{})
	yellowCardHome:=float64(0)
	yellowCardAway:=float64(0)
	redCardHome:=float64(0)
	redCardAway:=float64(0)
	firstGoalTime:=float64(0)
	firstGoalTeam:=float64(0)
	//Kind=1 进球，Kind=2 红牌，Kind=3 黄牌
	for _,v:=range listItems {
		item:=v.(map[string]interface{})
		if item["Kind"].(float64)==1{
			if item["isHome"].(float64)==1{
				firstGoalTeam=1
			}else{
				firstGoalTeam=2
			}
			firstGoalTime=item["happenTime"].(float64)
			break
		}
		if item["Kind"].(float64)==2{
			if item["isHome"].(float64)==1{
				redCardHome++
			}else{
				redCardAway++
			}
		}
		if item["Kind"].(float64)==3{
			if item["isHome"].(float64)==1{
				yellowCardHome++
			}else{
				yellowCardAway++
			}
		}
	}
	
	return true,[]float64{yellowCardHome,yellowCardAway,redCardHome,redCardAway,firstGoalTeam,firstGoalTime}
}
func FormatData(id string,league string,startAt string,t1 string,t2 string,text string,europe string,bf string,live string)(bool,[][]float64) {

	b,data:=leagueData(text)

	if !b{
		return false,nil
	}


	b2,data2:=betData(startAt,t1,t2,text)

	if !b2{
		return false,nil
	}
	b3,data3:=lastMatchData(league,t1,t2,text)
	if !b3{
		return false,nil
	}
	/*
	b5,data5:=bfData(bf)
	if !b5{
		return false,nil
	}
	b4,data4:=europeData(europe)
	if !b4{
		return false,nil
	}*/
	b6,data6:=liveData(live)
	if !b6{
		return false,nil
	}
	d:=[][]float64{
		data6,
		data,
		data2,
		data3[0],data3[1],data3[2],
		//data,
		//data2,

	}

return true,d
}